﻿using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsposeCombineThreadRun
{


    public class ThreadPoolExample
    {

        public static ReaderWriterLockSlim writeLock = new ReaderWriterLockSlim();
        private static string Title = "";
        static string TMPFILE = "out.csv";
        static string OUTFILE = "out.xls";
        private static int finished = 0;
        private static int currentCount = 0;
        static void Main(string[] args)
        {
            try
            {
                var path = "";
                if (args.Length == 0)
                {
                    path = ".";
                }
                else
                {
                    path = args[0];
                }
                if (File.Exists(OUTFILE))
                {
                    File.Delete(OUTFILE);
                }
                var files = Directory.GetFiles(path, "*.xls");
                var txts = ReadAllXlsDataAsCSVStrings(files);
                Console.WriteLine(txts.Last());
                foreach (var t in txts)
                {
                    Title += t;
                }

                File.WriteAllText(TMPFILE, Title);
                TxtLoadOptions lo = new TxtLoadOptions();
                lo.Encoding = Encoding.UTF8;
                Workbook workbook = new Workbook(TMPFILE, lo);
                workbook.FileFormat = FileFormatType.CSV;
                Worksheet worksheet = workbook.Worksheets[0];
                workbook.Save(OUTFILE);
                File.Delete(TMPFILE);
                Console.WriteLine("完成");

                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();
            }

        }

        private static string[] ReadAllXlsDataAsCSVStrings(string[] files)
        {
            string[] txts = new string[files.Length];
            Parallel.For(0, files.Length, (e) =>
            {
                currentCount++;
                var file = files[e];
                string txt = "";
                var rows = new Workbook(file).Worksheets[0].Cells.Rows;
                var fileInfo = new FileInfo(file);
                if (e == 0)
                {
                    Row row = rows[0];
                    for (int j = 0; j < row.LastDataCell.Column; j++)
                    {
                        if (j == 1)
                        {
                            Title += row.GetCellOrNull(j).DisplayStringValue + fileInfo.Name.Replace("xls", "osgb");
                            Title += ",";
                        }
                        else
                        {
                            Title += row.GetCellOrNull(j).DisplayStringValue;
                            Title += ",";
                        }
                    }

                    Title += "\r\n";
                    Console.WriteLine(Title);
                }

                for (var i = 1; i < rows.Count; i++)
                {
                    Row row = rows[i];
                    for (int j = 0; j < row.LastDataCell.Column; j++)
                    {
                        if (j == 1)
                        {
                            txt += row.GetCellOrNull(j).DisplayStringValue + fileInfo.Name.Replace("xls", "osgb");
                            txt += ",";
                        }
                        else
                        {
                            txt += row.GetCellOrNull(j).DisplayStringValue;
                            txt += ",";
                        }
                    }

                    txt += "\r\n";
                }

                try
                {
                    //File.WriteAllText("out/"+fileInfo.Name + ".csv", txt);
                    finished++;
                    txts[e] = txt;
                    var log = $"完成文件:{file},已完成{finished}/{files.Length},当前正在处理:{currentCount}";
                    currentCount--;
                    Console.WriteLine(log);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            });
            return txts;
        }
    }
}
